package com.yangzc.lion.search.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * jdbc连接数据库
 */
public class DBUtil {
	
	public DBUtil(){
		
	}
	
	/**
	 * 获取数据库连接
	 * @return
	 * @throws Exception
	 */
	public static Connection getMysqlConnection() throws Exception{
		ResourceBundle prop = ResourceBundle.getBundle("config/jdbc", Locale.getDefault());
		String url = prop.getString("jdbc.url");
		String userName = prop.getString("jdbc.username");
		String password = prop.getString("jdbc.password");
		Connection conn = null;
		try{  
			Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, userName, password);  
        }catch(Exception e){
        	e.printStackTrace();
        	if(conn !=null){
        		if(!conn.isClosed()){
            		conn.close();
            	}
            	conn = null;
        	}
            throw e;
        }  
		return conn;
	}
}
